


// import { ElLoading } from 'element-plus';
import DgLoad from "../dg-load"
import type { CLASLOADING } from "../dg-load"
import type { Ref } from "vue"
// 搜索显示的 loading
let searchLoad: Array<string> = ['/oper/sysrole/page', '/customer/bidGuarantee/projectList',"/customer/loanOrder/loanOrderList"]
// 不需加载loading
// let noneLoad: Array<string> = []
let noneLoad: Array<string> = ['/customer/common/getcaptcha']

let dgload = new DgLoad({ single: false })

export class AxiosLoad {
  public status = -1
  private loadingInstance?: CLASLOADING
  private loading: Ref<boolean>
  constructor(loading: Ref<boolean>) {
    this.loading = loading;
  }
  load(url: string | any): void {

    if (noneLoad.indexOf(url) > -1) return
    
    if (searchLoad.indexOf(url) > -1 || url.indexOf("/page") > -1 || url.indexOf("/list") > -1) {
      this.addLoad(1);
      return
    }
    this.addLoad(2);
  }
  addLoad(type: number): void {

    // if (this.status == 0) {
    //   return
    // }
    if (type == 1) {
      if (this.loading) {
        this.loading.value = true;
      }

    }

    if (type == 2) {
      if (this.loadingInstance) {
        this.loadingInstance.close();
      }
      Promise.resolve({ ok: 'ok' }).then(() => {
        this.status = 0;
        this.loadingInstance = dgload.loading({ text: '加载中...' })
        // this.loadingInstance = ElLoading.service({ text: '加载中...', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.5)' })
      })
    }

  }
  removeLoad(): void {
    this.status = 1
    if (this.loadingInstance) {
     this.loadingInstance.close();
    }
    if (this.loading) {
      this.loading.value = false;
    }
  }
}






